 /**
 * Description: 数据字典业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-17 上午 11:21:47
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-17   xk       0.0.1     Initial
 */
package com.metro.demo.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.common.utils.DicUtil;
import com.metro.demo.sys.mapper.IFunctionMapper;
import com.metro.utils.CollectionUtil;
import com.metro.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.demo.sys.mapper.ISysDictionaryMapper;
import com.metro.demo.sys.service.inf.ISysDictionaryService;
import com.metro.demo.sys.beans.vo.SysDictionary;
import java.util.List;

 /**
 * 数据字典业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-17]
 */
@Service("sysDictionaryService")
public class SysDictionaryService extends BaseServiceSupport<SysDictionary> implements ISysDictionaryService{
    //数据字典Mapper
    @Autowired
    private ISysDictionaryMapper sysDictionaryMapper;
    @Autowired
    private IFunctionMapper functionMapper;
    @Override
    public IMapper getMapper() {
        return sysDictionaryMapper;
    }

    @Override
    public IResult batchUpdateSysDictionary(List<SysDictionary> sysDictionarys) {
        try {
            if(CollectionUtil.isNotEmpty(sysDictionarys)){
                for(SysDictionary entity : sysDictionarys){
                    sysDictionaryMapper.updateNotEmpty(entity);
                }
                functionMapper.buidTreeTableInnerCode("sys_dictionary");
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改数据字典失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult save(SysDictionary entity) {
        try {
            sysDictionaryMapper.save(entity);
            functionMapper.buidTreeTableInnerCode("sys_dictionary");
            SysDictionary root = sysDictionaryMapper.getRootDictionary(entity);
            if(root != null){
                DicUtil.removeKeyFromCache(root.getCode());
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增数据字典失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysDictionary entity) {
        try {
            sysDictionaryMapper.update(entity);
            functionMapper.buidTreeTableInnerCode("sys_dictionary");
            SysDictionary root = sysDictionaryMapper.getRootDictionary(entity);
            if(root != null){
                DicUtil.removeKeyFromCache(root.getCode());
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改数据字典失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            SysDictionary entity = sysDictionaryMapper.getById(id);
            sysDictionaryMapper.deleteThisAndChild(entity);
            functionMapper.buidTreeTableInnerCode("sys_dictionary");
            SysDictionary root = sysDictionaryMapper.getRootDictionary(entity);
            if(root != null){
                DicUtil.removeKeyFromCache(root.getCode());
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            if(StringUtil.isNotEmpty(ids)){
                String[] idArr = ids.split(",");
                for(String id : idArr){
                    SysDictionary entity = new SysDictionary();
                    entity.setId(Long.valueOf(id));
                    SysDictionary root = sysDictionaryMapper.getRootDictionary(entity);
                    if(root != null){
                        DicUtil.removeKeyFromCache(root.getCode());
                    }
                }
                sysDictionaryMapper.deleteByIds(ids);
                functionMapper.buidTreeTableInnerCode("sys_dictionary");
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}